/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import Client.Socket.GestorComunicacionesViejo;
import Server.SocketManager;

/**
 *
 * @author Ibai
 */
public class IdentificationPanelViejo extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Server.SocketManager sm;
	private static boolean conectado;

	public JButton bIniciarSesion;
	public JButton bCancelar;
	private JTextField tfUser;
	private JPasswordField tfPass;

	public IdentificationPanelViejo() {

		JPanel panelUserPass = new JPanel();
		JPanel panelButtons = new JPanel(new FlowLayout(FlowLayout.CENTER));

		this.setSize(500, 250);
		this.setTitle("Ventana Logueo");
		this.setResizable(false);
		this.setLocation((getToolkit().getScreenSize().width - this.getBounds().width) / 2,
				(getToolkit().getScreenSize().height - this.getBounds().height) / 2);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		bIniciarSesion = new JButton("Iniciar Sesi\u00f3n");
		bCancelar = new JButton("Salir");

		tfUser = new JTextField("admin");
		tfPass = new JPasswordField("root");

		ArrayList<Component> labels = new ArrayList<Component>();
		labels.add(new JLabel("Usuario: "));
		labels.add(new JLabel("Contrase\u00f1a: "));
		ArrayList<Component> textFields = new ArrayList<Component>();
		textFields.add(tfUser);
		textFields.add(tfPass);

		panelUserPass.setBorder(BorderFactory.createTitledBorder("Identificaci\u00f3n"));
		panelAlign(panelUserPass, labels, textFields);

		panelButtons.add(bIniciarSesion);
		panelButtons.add(bCancelar);

		setLayout(new BorderLayout(3, 3));
		add(panelUserPass, BorderLayout.CENTER);
		add(panelButtons, BorderLayout.SOUTH);

		bIniciarSesion.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String pass = new String(tfPass.getPassword());
				if (tfUser.getText().equals("admin") && pass.equals("root")) {					
					try {
						sm = new SocketManager("127.0.0.1", 8423);
						conectado = true;
					} catch (IOException e) {
						conectado = false;
						JOptionPane.showMessageDialog(null, "Ha ocurrido un error al intentar conectarse al servidor.", "ERROR", JOptionPane.ERROR_MESSAGE);
					}
					if (conectado)
					{
						VendingPanel vp = new VendingPanel(sm);
						if (conectado)
						{
							vp.setVisible(true);
							Runnable procesarServidor = new GestorComunicacionesViejo(sm, vp);
							Thread hilo = new Thread(procesarServidor);
							hilo.start();
							dispose();
						}
					}
				}
				else {
					JOptionPane.showMessageDialog(null, "El usuario o la contrase\u00f1a son incorrectos", "ERROR", JOptionPane.ERROR_MESSAGE);
				}
			}
		});


		bCancelar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
	}

	private void panelAlign(Container p, ArrayList<Component> componentsFirstRow, ArrayList<Component> componentsSecondRow) {
		JPanel tempGlobalPanel = new JPanel();
		tempGlobalPanel.setLayout(new BoxLayout(tempGlobalPanel, BoxLayout.X_AXIS));

		JPanel tempPanelL = new JPanel();
		tempPanelL.setLayout(new BoxLayout(tempPanelL, BoxLayout.Y_AXIS));
		for (int i = 0; i < componentsFirstRow.size(); i++) {
			JPanel intPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 3, 3));
			intPanel.add(componentsFirstRow.get(i));
			tempPanelL.add(intPanel);
		}
		tempGlobalPanel.add(tempPanelL);

		JPanel tempPanelR = new JPanel();
		tempPanelR.setLayout(new BoxLayout(tempPanelR, BoxLayout.Y_AXIS));
		for (int i = 0; i < componentsSecondRow.size(); i++) {
			JPanel intPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 3, 3));
			intPanel.add(componentsSecondRow.get(i));
			tempPanelR.add(intPanel);
		}
		tempGlobalPanel.add(tempPanelR);

		p.add(tempGlobalPanel);

	}

	public JPasswordField getTfPass() {
		return tfPass;
	}

	public JTextField getTfUser() {
		return tfUser;
	}

	public static void main(String[]args){
		IdentificationPanelViejo id = new IdentificationPanelViejo();
		id.setVisible(true);
	}
}
